package games.graphPath;

import games.graphPath.Graph.Edge;
import games.graphPath.Graph.Vertex;

import java.util.ArrayList;

public class GraphPathAStarState implements Comparable<GraphPathAStarState>{

	private Graph _graph;
	
	private int _depth;
	
	private int _totalCost;
	
	private Vertex _cur;
	
	private int _moveCost;
	
	private String _hash;
	
	private GraphPathAStarState _parent; 
	
	public GraphPathAStarState(Graph g, GraphPathAStarState parent, int moveCost, int totalCost, int depth, Vertex cur){
		_parent = parent;
		_graph = g;
		_totalCost = totalCost;
		_cur = cur;
		_moveCost = moveCost;
		_depth = depth;
		_hash = ""+_cur.getIndex();
	}
	
	public int getCost() {
		return _totalCost;
	}

	public int getMoveCost() {
		return _moveCost;
	}
	
	public GraphPathAStarState getParent()
	{
		return _parent;
	}

	public ArrayList<GraphPathAStarState> getPossibleMoves() {
		ArrayList<Edge> n = (ArrayList<Edge>)_cur.getOutNeighbors();
		ArrayList<GraphPathAStarState> states = new ArrayList<GraphPathAStarState>(n.size());
		for (int i=0; i<n.size(); i++){
			Edge e = n.get(i);
			states.add(new GraphPathAStarState(_graph,this,e.getCost(),_totalCost+e.getCost(),_depth+1,e.getDst()));
		}
		return states;
	}

	public String hash() {
		return _hash;
	}

	public boolean isWin() {
		return (_cur.getIndex() == _graph.getDst().getIndex());
	}

	public int compareTo(GraphPathAStarState other) {
		return GraphHuristic.comapre(this, other);
	}

	public Vertex getCur() {
		return _cur;
	}

}
